package com.amazon.inapp.purchasing;

import android.os.RemoteException;
import com.amazon.android.Kiwi;
import com.amazon.android.framework.exception.KiwiException;
import com.amazon.android.framework.prompt.PromptContent;
import com.amazon.android.framework.prompt.PromptManager;
import com.amazon.android.framework.task.command.AbstractCommandTask;
import com.amazon.android.licensing.LicenseFailurePromptContentMapper;
import com.amazon.venezia.command.FailureResult;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

abstract class KiwiBaseCommandTask extends AbstractCommandTask
{
  private static final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";
  protected static final String FALSE = "false";
  protected static final String KEY_CURSOR = "cursor";
  protected static final String KEY_DESCRIPTION = "description";
  protected static final String KEY_ERROR_MESSAGE = "errorMessage";
  protected static final String KEY_HAS_CONTENT = "hasContent";
  protected static final String KEY_HAS_MORE = "hasMore";
  protected static final String KEY_ICON_URL = "iconUrl";
  protected static final String KEY_ITEM_TYPE = "itemType";
  protected static final String KEY_ORDER_STATUS = "orderStatus";
  protected static final String KEY_PERIOD_DATE_END = "endDate";
  protected static final String KEY_PERIOD_DATE_START = "startDate";
  protected static final String KEY_PRICE = "price";
  protected static final String KEY_PURCHASE_ITEM_INTENT = "purchaseItemIntent";
  protected static final String KEY_RECEIPT = "receipt";
  protected static final String KEY_RECEIPTS = "receipts";
  protected static final String KEY_REQUEST_ID = "requestId";
  protected static final String KEY_REVOCATIONS = "revocations";
  protected static final String KEY_SDK_VERSION = "sdkVersion";
  protected static final String KEY_SIGNATURE = "signature";
  protected static final String KEY_SKU = "sku";
  protected static final String KEY_SKUS = "skus";
  protected static final String KEY_TITLE = "title";
  protected static final String KEY_TOKEN = "token";
  protected static final String KEY_USER_ID = "userId";
  protected static final String SDK_VERSION = "1.0";
  private static final String TAG = "KiwiBaseCommandTask";
  protected static final String TRUE = "true";
  private final Map<String, Object> _commandData;
  private final String _commandName;
  private final String _commandVersion;
  private final String _requestId;
  private LicenseFailurePromptContentMapper mapper = new LicenseFailurePromptContentMapper();

  KiwiBaseCommandTask(String paramString1, String paramString2, String paramString3)
  {
    this._requestId = paramString3;
    this._commandName = paramString1;
    this._commandVersion = paramString2;
    this._commandData = new HashMap();
    this._commandData.put("requestId", this._requestId);
    this._commandData.put("sdkVersion", "1.0");
  }

  protected void addCommandData(String paramString, Object paramObject)
  {
    this._commandData.put(paramString, paramObject);
  }

  protected Map<String, Object> getCommandData()
  {
    return this._commandData;
  }

  protected String getCommandName()
  {
    return this._commandName;
  }

  protected String getCommandVersion()
  {
    return this._commandVersion;
  }

  protected Date getDateFromString(String paramString)
    throws JSONException
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    try
    {
      Date localDate = localSimpleDateFormat.parse(paramString);
      long l = localDate.getTime();
      if (0L == l)
        localDate = null;
      return localDate;
    }
    catch (ParseException localParseException)
    {
      throw new JSONException(localParseException.getMessage());
    }
  }

  protected Receipt getReceiptFromReceiptJson(JSONObject paramJSONObject)
    throws JSONException
  {
    String str1 = paramJSONObject.getString("sku");
    Item.ItemType localItemType = Item.ItemType.valueOf(paramJSONObject.getString("itemType"));
    String str2 = paramJSONObject.getString("token");
    if (Item.ItemType.SUBSCRIPTION == localItemType);
    for (SubscriptionPeriod localSubscriptionPeriod = getSubscriptionPeriodFromReceiptJson(paramJSONObject); ; localSubscriptionPeriod = null)
      return new Receipt(str1, localItemType, false, localSubscriptionPeriod, str2);
  }

  protected String getRequestId()
  {
    return this._requestId;
  }

  protected SubscriptionPeriod getSubscriptionPeriodFromReceiptJson(JSONObject paramJSONObject)
    throws JSONException
  {
    if (!paramJSONObject.has("startDate"))
      return null;
    Date localDate1 = getDateFromString(paramJSONObject.getString("startDate"));
    String str = paramJSONObject.optString("endDate");
    boolean bool = isNullOrEmpty(str);
    Date localDate2 = null;
    if (bool);
    while (true)
    {
      return new SubscriptionPeriod(localDate1, localDate2);
      localDate2 = getDateFromString(str);
    }
  }

  protected boolean isExecutionNeeded()
  {
    return true;
  }

  protected boolean isNullOrEmpty(String paramString)
  {
    return (paramString == null) || (paramString.trim().length() == 0);
  }

  protected void onException(KiwiException paramKiwiException)
  {
    if (Logger.isTraceOn())
      Logger.trace("KiwiBaseCommandTask", "onException, result: " + paramKiwiException.getMessage());
    PromptManager localPromptManager = Kiwi.getPromptManager();
    PromptContent localPromptContent = this.mapper.map(paramKiwiException);
    if (localPromptContent != null)
      localPromptManager.present(new FailurePrompt(localPromptContent));
    sendFailedResponse();
  }

  protected void onFailure(FailureResult paramFailureResult)
    throws RemoteException, KiwiException
  {
    if (paramFailureResult == null)
    {
      if (Logger.isTraceOn())
        Logger.trace("KiwiBaseCommandTask", "onFailure: null result");
      return;
    }
    if (Logger.isTraceOn())
      Logger.trace("KiwiBaseCommandTask", "onFailure: result message: " + paramFailureResult.getDisplayableMessage());
    FailurePrompt localFailurePrompt = new FailurePrompt(new PromptContent(paramFailureResult.getDisplayableName(), paramFailureResult.getDisplayableMessage(), paramFailureResult.getButtonLabel(), paramFailureResult.show()));
    Kiwi.getPromptManager().present(localFailurePrompt);
    sendFailedResponse();
  }

  protected void postRunnableToMainLooper(Runnable paramRunnable)
  {
    HandlerManager.getMainHandlerAdapter().post(paramRunnable);
  }

  protected abstract void sendFailedResponse();

  protected boolean verifyReceipt(String paramString, Receipt paramReceipt, JSONObject paramJSONObject)
  {
    if (Logger.isTraceOn())
      Logger.trace("KiwiBaseCommandTask", "Validating receipt: " + paramReceipt);
    String str1 = paramJSONObject.optString("signature");
    boolean bool3;
    String str2;
    boolean bool2;
    if (!isNullOrEmpty(str1))
    {
      bool3 = verifySignature(paramString, paramReceipt.getPurchaseToken(), str1);
      if (!Logger.isTraceOn())
        break label178;
      StringBuilder localStringBuilder = new StringBuilder().append("signature verification ");
      if (bool3)
      {
        str2 = "succeeded";
        Logger.error("KiwiBaseCommandTask", str2 + " for request ID " + getRequestId());
        bool2 = bool3;
      }
    }
    boolean bool1;
    do
    {
      return bool2;
      str2 = "failed";
      break;
      bool1 = Logger.isTraceOn();
      bool2 = false;
    }
    while (!bool1);
    Logger.error("KiwiBaseCommandTask", "a signature was not found in the receipt for request ID " + getRequestId());
    return false;
    label178: return bool3;
  }

  protected boolean verifySignature(String paramString1, String paramString2, String paramString3)
  {
    return Kiwi.isSignedByKiwi(paramString1 + "-" + paramString2, paramString3);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.inapp.purchasing.KiwiBaseCommandTask
 * JD-Core Version:    0.6.2
 */